<?php
/**
 * Nimo: 邮件类 应用开源项目PHPMailer实现邮件发送功能
 * @author andery <andery@foxmail.com>
 * @link http://www.minophp.com/
 * @version $Id: CMail.lib.php 2009-08-02 15:50:32Z andery $
 */
class CMail
{
    // 扩展类名
    public $vd_name = 'PHPMailer';
    // mailer对象
    public $mailer;
    // 错误信息
    public $errors = array();
    
    /**
     * 构造函数
     * @param array $mailer_gw
     * @return void;
     */
    public function __construct($mailer_gw)
    {
        include_once(NIMO_VENDORS . '/' . Nimo::$_vendors[$this->vd_name]);
        eval('$this->mailer = new ' . $this->vd_name . '('.$mailer_gw['SMTPDebug'].');');
        $this->mailer->SetLanguage('zh_cn', NIMO_VENDORS . '/' . $this->vd_name .'/language/');
        $this->mailer->Timeout = $mailer_gw['Timeout'];
        if (!isset($mailer_gw['Mailer']) || $mailer_gw['Mailer'] == 'mail') {
            $this->mailer->IsMail(); 
        } else {
            $mailer_gw = $this->init($mailer_gw);
            foreach ($mailer_gw as $key=>$val) {
        	    $this->mailer->$key = $val;
            }
        }
    }
    
    /**
     * 发送邮件方法
     * @param mixed $toaddress 收件人地址
     *        需要收件人姓名：$toaddress = arary('$name'=>'@email')
     * @param string $subject 邮件标题
     * @param string $body 邮件主题
     * @param string $charset 编码方式
     * @param bool $is_html 是否html格式
     * @param mixed $attachs 附件
     *        需要附件名：$attachs = arary('$name'=>'$file')
     * @return boolean
     */
    public function send($toaddress, $subject, $body, $charset, $is_html, $attachs = false)
    {
        //$this->mailer->Priority  = $this->priority; //邮件优先级
        $this->mailer->CharSet   = $charset;
        $this->mailer->IsHTML($is_html);
        $this->mailer->Subject   = $subject;
        $this->mailer->Body      = $body;
        if ($attachs) {
            $this->mailer->ClearAttachments();//清除历史附件
            if (is_array($attachs)) {
        	    foreach ($attachs as $name=>$file) {
        	        $this->mailer->AddAttachment($file, $name); 
        	    }
            } else {
                $this->mailer->AddAttachment($attachs, $attachs);
            }
        }
        
        $this->mailer->ClearAddresses(); //清除历史收件人
        if (is_array($toaddress)) {
            foreach ($toaddress as $name=>$mail) {
                $this->mailer->AddAddress($mail, $name);
            }
        } else {
            $this->mailer->AddAddress($toaddress);
        }
        $send_result = $this->mailer->Send();
        if (!$send_result) {
            $this->errors[] = $this->mailer->ErrorInfo;
        }
        return $send_result;
    }
    
    /**
     * 初始化邮件发送网关信息
     * @param array $mailer_gw
     * @return arrray
     */
    private function initGw($mailer_gw)
    {
        // 初始化网关信息
        $init_gw = array(
            'Mailer'=>'mail',
            'From' => '',
            'FromName' => 'Nimo root',
            'Host' => 'localhost',
            'Port' => '25',
            'SMTPAuth' => false,
            'Username' => '',
            'Password' => '',
            'Timeout' => 30,
            'SMTPDebug' => false,
        );
        if (is_array($mailer_gw)) {
        	// 避免引入phpmailer以外属性名造成错误，先取交集
        	$mailer_gw = array_intersect_key($mailer_gw, $init_gw);
            return array_merge($init_gw, $mailer_gw);
        } else {
            throw new CException(Nimo::lang('mail_gateway_error'));
        }
    }
}
